import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    private Queue<Integer> qu1 = new LinkedList<>();
    private Queue<Integer> qu2 = new LinkedList<>();
    
    public MyStack() {

    }
    
    public void push(int x) {
    //哪一个队列不为空就入到哪个队列中
          if(empty()){
              qu1.offer(x);
          }else if(!qu1.isEmpty()){
              qu1.offer(x);
          }else {
              qu2.offer(x);
          }
    }
    //移除并且返回栈顶元素
    public int pop() {
        if(empty()){//都为空无法返回元素
            return -1;
        }
        //如果一个队列不为空那就把这个队列中size-1个元素添加到另外一个队列中
        if(!qu1.isEmpty()){
            int sz = qu1.size();
            for(int i =0;i<sz-1;i++){
                qu2.offer(qu1.poll());
            }
            return qu1.poll();
        }else {
             int sz = qu2.size();
             for(int i =0;i<sz-1;++i){
                 qu1.offer(qu2.poll());
             }
             return qu2.poll();
        }
    }
    
    public int top() {
        if(empty()){//都为空无法返回元素
            return -1;
        }
        //如果一个队列不为空那就把这个队列中size-1个元素添加到另外一个队列中
        if(!qu1.isEmpty()){
            int sz = qu1.size();
            int tmp =-1;
            for(int i =0;i<sz;i++){
                tmp =qu1.poll();
                qu2.offer(tmp);
            }
            return tmp;
        }else {
             int sz = qu2.size();
             int tmp =-1;
             for(int i =0;i<sz-1;++i){
                 tmp = qu2.poll();
                 qu1.offer(tmp);
             }
             return tmp;
        }
    }
    
    public boolean empty() {
         if(qu1.isEmpty()&&qu2.isEmpty()){
             return true;
         }
         return false;
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */